package cn.flant.exec.chain;

import java.util.LinkedList;

import cn.flant.log.Logger;

public class ExecutorChain<T> {
	private Logger log = Logger.getLogger(ExecutorChain.class);
	private LinkedList<Executor<T>> executorList = new LinkedList<Executor<T>>();

	public synchronized void execute(T object) {
		if (object == null) {
			return;
		}
		for (Executor<T> executor : executorList) {
			try {
				executor.execute(object);
			} catch (Throwable e) {
				log.error("[" + executor + "] execute failed", e);
			}
		}
	}

	public synchronized void addExecutor(Executor<T> executor) {
		if (executor == null) {
			return;
		}
		executorList.add(executor);
	}

	public synchronized void removeExecutor(Executor<T> executor) {
		if (executor == null) {
			return;
		}
		executorList.remove(executor);
	}
}
